﻿@namespace Blazorise.Docs.Docs.Examples
@using System.IO

<Field>
    <FileEdit Changed="@OnChanged" Written="@OnWritten" Progressed="@OnProgressed" />
</Field>

@code {
    async Task OnChanged( FileChangedEventArgs e )
    {
        try
        {
            var file = e.Files.FirstOrDefault();
            if ( file == null )
            {
                return;
            }

            using ( MemoryStream result = new MemoryStream() )
            {
                await file.OpenReadStream( long.MaxValue ).CopyToAsync( result );
            }
        }
        catch ( Exception exc )
        {
            Console.WriteLine( exc.Message );
        }
        finally
        {
            this.StateHasChanged();
        }
    }

    void OnWritten( FileWrittenEventArgs e )
    {
        Console.WriteLine( $"File: {e.File.Name} Position: {e.Position} Data: {Convert.ToBase64String( e.Data )}" );
    }

    void OnProgressed( FileProgressedEventArgs e )
    {
        Console.WriteLine( $"File: {e.File.Name} Progress: {e.Percentage}" );
    }
}